`

$ ps -ef

$ echo $?

0

Exit codes are important when you have a series of scripts that

call one another. You may have a workflow where one script

invokes another depending on a state of a specific condition.

Exercise 1: Recording Your Name and the Date

To practice what you’ve learned in this chapter, we encourage

you to write a script that does the following:

1. Accepts two arguments on the command line and assigns them to

variables. The first argument should be your first name and the

second should be your last name.

2. Creates a new file named output.txt.

3. Writes the current date to output.txt using the date command.

(Bonus points if you can make the date command print the date

in the DD-MM-YYYY format; use man date to learn how this

works.)

4. Writes your full name to output.txt.

5. Makes a backup copy of output.txt, named backup.txt, using the

cp command. (Use man cp if you arent sure of the command’s

syntax.)

6. Prints the content of output.txt file to the standard output stream.

An example solution can be found at

https://github.com/dolevf/Black-Hat-

Bash/blob/master/ch01/exercise_solution.sh.

Summary

In this chapter, you ran simple Linux commands in the terminal

and learned about command options using the man command. You

also learned how to pass arguments to scripts and execute a sequence

of commands from within scripts. We covered the fundamentals of

bash, such as how to write basic programs that make use of

variables, arrays, redirects, exit codes, arguments, and more. We

Black Hat Bash (Early Access) © 2023 by Dolev Farhi and Nick Aleks